<template>
  <div class="login">
    <van-nav-bar
      title="登录"
      class="page-nav-bar"
    >
      <template #left>
        <van-icon color="#fff" @click="$router.back()" name="cross" />
      </template>
    </van-nav-bar>
    <!-- 表单 -->
    <van-form @submit="onSubmit" ref="form">
      <van-field
        v-model="form.mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="[
        { required: true, message: '请填写手机号' },
        {pattern:/^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,message:'请输入正确的手机号'}
        ]"
      >
        <template #left-icon>
          <i class="toutiao toutiao-shouji"></i>
        </template>
      </van-field>
      <van-field
        v-model="form.code"
        name="code"
        placeholder="请输入验证码"
        :rules="[
        { required: true, message: '请填写验证码' },
        {pattern:/^[0-9]{6}$/,message:'请输入正确的验证码'}
        ]"
      >
        <template #left-icon>
          <i class="toutiao toutiao-yanzhengma"></i>
        </template>
        <!-- native-type="button"，变成普通按钮 -->
        <template #button>
          <!-- 发送验证码倒计时 -->
          <van-count-down @finish="isShowCountDown=false" v-if="isShowCountDown" :time="6*1000" format="ss s" />
          <van-button
          v-else
          @click="onSend"
          native-type="button"
          class="send-btn"
          size="small"
          type="primary"
          >
          发送验证码
          </van-button>
        </template>
      </van-field>
      <div style="margin: 16px;">
        <van-button round block class="login-btn" type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { Toast } from 'vant'
import { getCode, login } from '@/api/user.js'
export default {
  name: 'Login',
  data () {
    return {
      form: {
        mobile: '',
        code: ''
      },
      isShowCountDown: false
    }
  },

  created () {

  },

  methods: {
    async onSubmit () {
      // console.log('提交表单')
      try {
        const res = await login(this.form)
        this.$store.commit('initUser', res)
        this.$router.push('/profile')
      } catch (error) {
        Toast.fail('登录失败')
      }
    },
    async onSend () {
      try {
        await this.$refs.form.validate('mobile')
        // console.log('发送验证码')
        await getCode(this.form.mobile)
        Toast.success('发送成功')
        this.isShowCountDown = true
      } catch (err) {
        if (!err.response) return
        if (err.response.status === 429) {
          return Toast.fail('亲，不要发送太频繁哦')
        }
        Toast.fail('网络异常')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.login{
  .login-btn{
    width: 694px;
    height: 88px;
    background: #6DB4FB;
    border: none;
    border-radius: 10px;
  }
  .toutiao{
    font-size: 37px;
  }
  .send-btn{
    width: 152px;
    height: 46px;
    background: #EDEDED;
    border: none;
    border-radius: 23px;
    padding: 0;
    font-size: 22px;
    color: #666666;
  }
}
</style>
